﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using Hyt.Core.Authentication;
using Hyt.Core.Cache;
using Hyt.Core.Interceptors;
using Hyt.Core.Security;

namespace Hyt.Core
{
    public abstract class IocSetting : IIocSetting
    {
        public void Register(ContainerBuilder builder, IUnitOfWork unitOfWork)
        {
            RegisterAuthManager(builder);
            RegisterAuthInterceptor(builder, unitOfWork);
            RegisterICacheProvider(builder);
            RegisterPrincipalResolver(builder, unitOfWork);
        }

        /// <summary>
        /// 授权管理器
        /// </summary>
        /// <param name="builder"></param>
        public virtual void RegisterAuthManager(ContainerBuilder builder)
        {
            builder.RegisterType<AuthManager>().As<IAuthManager>().PropertiesAutowired().OnActivating(t =>
            {
                var rules = GetAuthRules();
                if (rules != null)
                {
                    rules.ForEach(r => t.Instance.AddRule(r));
                }
            });
        }

        /// <summary>
        /// 授权拦截器
        /// </summary>
        /// <param name="builder"></param>
        public virtual void RegisterAuthInterceptor(ContainerBuilder builder, IUnitOfWork unitOfWork)
        {
            builder.RegisterType<AuthInterceptor>().PropertiesAutowired().OnActivating(t=>t.Instance.Principal=unitOfWork.GetCurrentPrincipal());
        }

        /// <summary>
        /// 缓存管理器
        /// </summary>
        /// <param name="builder"></param>
        public virtual void RegisterICacheProvider(ContainerBuilder builder)
        {
            builder.RegisterType(GetCacheProviderType()).As<ICacheProvider>().PropertiesAutowired();
        }

        /// <summary>
        /// 安全主体解析器
        /// </summary>
        /// <param name="builder"></param>
        public virtual void RegisterPrincipalResolver(ContainerBuilder builder, IUnitOfWork unitOfWork)
        {
            builder.RegisterType<PrincipalResolver>().As<IPrincipalResolver>().PropertiesAutowired().OnActivating(t =>
            {
                var resolvers = GetCustomPrincipalResolvers(builder, unitOfWork);
                if (resolvers!=null)
                {
                    resolvers.ForEach(r=>t.Instance.AddResolver(r));
                }
            });
        }

        /// <summary>
        /// 授权规则
        /// </summary>
        /// <returns></returns>
        public abstract List<IAuthRule> GetAuthRules();

        /// <summary>
        /// 缓存管理器
        /// </summary>
        /// <returns></returns>
        public abstract Type GetCacheProviderType();

        /// <summary>
        /// 获取具体类主体解析器
        /// </summary>
        /// <returns></returns>
        public abstract List<ICustomPrincipalResolver> GetCustomPrincipalResolvers(ContainerBuilder builder, IUnitOfWork unitOfWork);
    }
}
